package com.company.array;

/**
 * @author jrue
 * @description https://leetcode-cn.com/problems/maximum-length-of-repeated-subarray/
 * @date 2019/11/15 16:43
 * 给两个整数数组 A 和 B ，返回两个数组中公共的、长度最长的子数组的长度。
 */
public class FindLength {

    public static void main(String[] args) {
        System.out.println(findLength(new int[]{1, 2, 3, 2, 1}, new int[]{3, 2, 1, 4, 7}));
    }

    public static int findLength(int[] A, int[] B) {
        int[][] dp = new int[A.length][B.length];
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < A.length; i++) {
            for (int j = 0; j < B.length; j++) {
                if (A[i] == B[j]) {

                    if (i == 0 || j == 0 ) dp[i][j] = 1;
                    else {
                        dp[i][j] = dp[i - 1][j - 1] + 1;
                    }
                    if (max < dp[i][j]) max = dp[i][j];

                }
            }
        }
        return max == Integer.MIN_VALUE ? 0 : max;

    }

}
